package cn.sanch.org.controller;

import cn.sanch.basic.util.AjaxResult;
import cn.sanch.basic.util.BusinessException;
import cn.sanch.basic.util.ExcelUtil;
import cn.sanch.basic.util.PageList;
import cn.sanch.org.domain.Shop;
import cn.sanch.org.domain.ShopAuditLog;
import cn.sanch.org.dto.ShopDto;
import cn.sanch.org.query.ShopQuery;
import cn.sanch.org.service.IShopService;
import com.sun.deploy.net.HttpResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 接口类：店铺
 * @author sanch37
 * 2022/5/28
 */
@Api(value = "宠物之家的店铺相关API", description = "店铺的CRUD操作")
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 接口：查询店铺单个对象
     * @param id Long id
     * @return Shop
     */
    @ApiOperation(value = "查询店铺单个对象")
    @GetMapping("/{id}")
    public Shop queryOne(@PathVariable("id") Long id) {
        return shopService.findOne(id);
    }


    /**
     * 接口：查询店铺所有对象
     * @return List<Shop>
     */
    @ApiOperation(value = "查询店铺所有对象")
    @GetMapping
    public List<Shop> queryAll() {
        return shopService.findAll();
    }


    /**
     * 接口：删除单个店铺对象
     * @param id Long id
     * @return AjaxResult
     */
    @ApiOperation(value = "删除单个店铺对象")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败！");
        }
    }

    /**
     * 接口：高级查询 + 分页查询
     * @param shopQuery ShopQuery shopQuery
     * @return PageList<Shop>
     */
    @ApiOperation(value = "高级查询 + 分页查询")
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery) {
        return shopService.queryPage(shopQuery);
    }

    /**
     * 接口：店铺添加或修改
     * @param shop
     * @return
     */
    @ApiOperation(value = "店铺添加或修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            /* 没有id为添加 */
            if (shop.getId() == null) {
                shopService.save(shop);
            /* 有id为修改 */
            }else {
                shopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
     * 接口：批量删除店铺
     * @param ids Long[] ids
     * @return AjaxResult
     *
     *
     */
    @ApiOperation(value = "批量删除店铺")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody Long[] ids) {
        try {
            shopService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }


    /**
     * 接口：商家入驻
     * @param shop
     * @return
     */
    @ApiOperation(value = "商家入驻")
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("入驻失败！" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }


    /**
     * 接口：店铺审核通过
     * @param log
     * @return
     */
    @ApiOperation(value = "店铺审核通过")
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog log) {
        try {
            shopService.pass(log);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("审核通过失败！");
        }
    }


    /**
     * 接口：店铺审核驳回
     * @param log
     * @return
     */
    @ApiOperation(value = "店铺审核驳回")
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log) {
        try {
            shopService.reject(log);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("审核驳回失败！");
        }
    }

    /**
     * 接口：导出Excel表
     * @param response
     */
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) {

        //查询所有店铺对象：导出的数据
        List<Shop> shops = shopService.findAll();
        //shops, - 导出的数据
        // null, - 标题：无标题
        // "店铺信息", - sheet页名
        // Shop.class, - 导出时映射的文件
        // "shop.xlsx", - 导出的文件名及格式
        // response - 导出时的响应对象
        ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);

    }

    /**
     * 接口：导入文件
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart MultipartFile file) {

        //file, - 要导入的文件：前端传入的文件
        // 0, - 标题函数：0
        // 1, - 头函数：1
        // Shop.class - 导入时映射的文件
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        for (Shop shop : shops) {
            shopService.save(shop);
        }
    }


    /**
     * 接口：获取店铺每一种状态的数量
     * @return
     */
    @GetMapping("/echarts")
    public List<ShopDto> getShopStates() {
        return shopService.getShopStates()  ;
    }


    /**
     * 接口：用户点击激活发送请求，跳转到登录界面
     * @return
     */
    @GetMapping("/activate/{id}")
    public AjaxResult toLogin(@PathVariable("id") Long id) {
        try {
            //通过审核通过操作的发送的邮件的店铺id找到店铺对象
            Shop shop = shopService.findOne(id);
            //设置店铺状态为审核通过
            shop.setState(3);
            //修改店铺信息
            shopService.update(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("激活失败！");
        }

    }
}
